package com.bz.edu.info.manager.domin;

import java.util.ArrayList;
import java.util.Scanner;

public class test001 {
    public static void main(String[] args) {
        ArrayList<Student> stuList = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        lo:
        while(true) {
            System.out.println("---------欢迎来到学生学籍管理系统--------------");
            System.out.println("请输入你的选择");
            System.out.println("1.增加新学生信息");
            System.out.println("2.修改学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.查看所有学生信息");
            System.out.println("5.退出系统");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent(stuList);
                    break;
                case "2":
                    updateStudent(stuList);
                    break;
                case "3":
                    deleteStudent(stuList);
                    break;
                case "4":
                    queryStudent(stuList);
                    break;
                case "5":
                    System.out.println("感谢你的使用");
                    break lo;
                default:
                    System.out.println("你的输入有误!!!");
                    break;
            }
        }
    }
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入学号");
        int stuNum  = sc1.nextInt();
        int index = getIndex(list,stuNum);
        if(index == -1)
        {
            System.out.println("请输入名字");
            String name = sc1.next();
            Student stu = new Student(stuNum,name);
            list.add(stu);
        }
        else {
            System.out.println("此学号已存在，无法添加");
        }
    }
    public static void deleteStudent(ArrayList<Student> list)
    {
        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入需要删除学号");
        int stuNum  = sc1.nextInt();
        int index = getIndex(list,stuNum);
        if(index== -1)
        {
            System.out.println("输入的学号不存在");
        }
        else{
            list.remove(index);
            System.out.println("删除成功！");
            }
    }
    public static void updateStudent(ArrayList<Student> list)
    {
        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入需要修改的学号");
        int stuNum  = sc1.nextInt();
        int index = getIndex(list,stuNum);
        if(index== -1)
        {
            System.out.println("输入的学号不存在");
        }
        else{
            System.out.println("请输入需要修改的姓名");
            String name = sc1.next();
            Student stu = new Student(stuNum,name);
            list.set(index,stu);
            System.out.println("修改成功！");
        }
    }
    public  static void queryStudent(ArrayList<Student> list)
    {
        if(list.size()>0) {
            for (Student stu : list) {
                int stuNum = stu.getStuNum();
                String name = stu.getName();
                System.out.println("学生学号：" + stuNum + ",学生姓名：" + name);
            }
        }
        else
        {
            System.out.println("系统不存在任何学生信息");
        }
    }
    public static int getIndex(ArrayList<Student> list, int stuNum)
    {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            int stuNumReal = stu.getStuNum();
            if(stuNum == stuNumReal)
            {
                index = i;
            }
        }
        return  index;
    }
}


